package com.ulic.customerManApplication.membermodule.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ulic.customerManApplication.base.BaseAction;
import com.ulic.customerManApplication.commonmodule.bo.Organization;
import com.ulic.customerManApplication.constant.CONSTANT;
import com.ulic.customerManApplication.membermodule.bo.Member;
import com.ulic.customerManApplication.membermodule.service.MemberService;
import com.ulic.customerManApplication.membermodule.vo.MemberVO;
import com.ulic.customerManApplication.pager.PagerModel;
import com.ulic.customerManApplication.usermodule.bo.User;
import com.ulic.customerManApplication.util.excel.DownloadExcel;
import com.ulic.customerManApplication.util.excel.ExpMembers;
@Component("memberAction")
@Scope("prototype")
public class MemberAction extends BaseAction{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
    private MemberService memberService;
    private Member member;
    private User user;
    private String startcreate;
    private String endcreate;
    
    private String subCityHidden;
	
	public String getSubCityHidden() {
		return subCityHidden;
	}
	public void setSubCityHidden(String subCityHidden) {
		this.subCityHidden = subCityHidden;
	}
    
	public String getStartcreate() {
		return startcreate;
	}
	public void setStartcreate(String startcreate) {
		this.startcreate = startcreate;
	}
	public String getEndcreate() {
		return endcreate;
	}
	public void setEndcreate(String endcreate) {
		this.endcreate = endcreate;
	}
	public MemberService getMemberService() {
		return memberService;
	}
    @Resource
	public void setMemberService(MemberService memberService) {
		this.memberService = memberService;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	 
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	 
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		PagerModel pm = memberService.getMembers(curPage);
		httpServletRequest.setAttribute("pm",pm);
		return SUCCESS;
	}
	
	public String searchMembers(){
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		String buttonName = httpServletRequest.getParameter("buttonType");
		if(!"exp".equals(buttonName)){
		
		MemberVO memberVO = new MemberVO();
		String roleName = (String)session.get("rolename");
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		memberVO.setRoleName(roleName);
		if(member==null){
			member = new Member();
		}
		if("SUBADMIN".equals(roleName)){
			 Organization organ = (Organization)session.get("barchList");
			 if(organ!=null){
				 member.setSubCompany(organ.getUm_organ_id());
			 }
		}else if("THIRDADMIN".equals(roleName)){
			 Organization subComOrgan = (Organization)session.get("barchList");
			 if(subComOrgan!=null){
				 member.setSubCompany(subComOrgan.getUm_organ_id());
			 }
			Organization organ = (Organization)session.get("expensedList");
			 if(organ!=null){
				 member.setSubCity(organ.getUm_organ_id());
			 }
		} 
		memberVO.setMember(member);
		memberVO.setUser(user);
		memberVO.setStartcreate(startcreate);
		memberVO.setEndcreate(endcreate);
		PagerModel pm = memberService.getMembers(memberVO,curPage);
		httpServletRequest.setAttribute("pm",pm);
		httpServletRequest.setAttribute("member", member);
		httpServletRequest.setAttribute("roleName", roleName);
		return SUCCESS;
		}else{
			return expMembers();
		}
	}
	
	public String viewMember()throws Exception{
		member = memberService.getMember(member.getId());
		return SUCCESS;
	}
	public String updateMember()throws Exception{
		member = memberService.getMember(member.getId());
		return SUCCESS;
	}
	
    public String updatedMember()throws Exception{
			 User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
			 member.setUser(user);
			 memberService.modifyMember(member);
			 return SUCCESS;
	}
		
	public String deleteMember()throws Exception{
		member = memberService.getMember(member.getId());
		memberService.removeMember(member);
		recache();
		return SUCCESS;
	}
	
	@SuppressWarnings("deprecation")
	public String expMembers(){
		HttpServletRequest httpServletRequest = ServletActionContext. getRequest();
		HttpServletResponse httpServletResponse = ServletActionContext.getResponse();
		MemberVO memberVO = new MemberVO();
		String roleName = (String)session.get("rolename");
		User user = (User)session.get(CONSTANT.USER_SESSION_KEY);
		memberVO.setRoleName(roleName);
		if(member==null){
			member = new Member();
		}
		if("SUBADMIN".equals(roleName)){
			 Organization organ = (Organization)session.get("barchList");
			 if(organ!=null){
				 member.setSubCompany(organ.getUm_organ_id());
			 }
		}else if("THIRDADMIN".equals(roleName)){
			 Organization subComOrgan = (Organization)session.get("barchList");
			 if(subComOrgan!=null){
				 member.setSubCompany(subComOrgan.getUm_organ_id());
			 }
			Organization organ = (Organization)session.get("expensedList");
			 if(organ!=null){
				 member.setSubCity(organ.getUm_organ_id());
			 }
		} 
		memberVO.setMember(member);
		memberVO.setUser(user);
		memberVO.setStartcreate(startcreate);
		memberVO.setEndcreate(endcreate);
//		PagerModel pm = memberService.getMembers(memberVO,curPage);
		String realPath = httpServletRequest.getRealPath("/download/");
		String fileName = "会员信息.xls";
		String[] headers = {"分公司","中支","姓名","性别","身份证号","生日","手机","电子邮件","会员类型","车辆品牌","兴趣爱好","捐助学生数","捐赠记录","转介绍记录","发帖记录","备注"}; 
		ExpMembers.exp(headers,memberService.getMembers(memberVO),realPath+"/"+fileName);//将数据读取到服务器上的文件中
	    DownloadExcel.download(realPath,fileName,httpServletRequest,httpServletResponse);//从服务器上下载文件
		return null;
	}
	
	
}
